//
//  WHBankInfoController.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/14.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHBankInfoController.h"
#import "WHBankInfoView.h"
#import "WHChangeBankController.h"

@interface WHBankInfoController ()

@property (nonatomic, strong) WHBankInfoView *bankInfoView;

@end

@implementation WHBankInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"银行卡信息";
    [self setUpNavigationItem];
    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self selectBankCard];
}

- (void)setUpNavigationItem {
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    rightButton.frame = CGRectMake(0, 0, 40, kNavBarHeight);
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"换绑" attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(16)), NSForegroundColorAttributeName:CMDarkOrangeColor}];
    [rightButton setAttributedTitle:attStr forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(changeBank:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)setUpUI {
    WHBankInfoView *bankInfoView = [[WHBankInfoView alloc] init];
    _bankInfoView = bankInfoView;
    [self.view addSubview:bankInfoView];
    [bankInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top).offset(kTopHeight);
        make.height.mas_equalTo(kRealwidth(210));
    }];
}

- (void)changeBank:(UIButton *)button {
    WHChangeBankController *changeBank = [[WHChangeBankController alloc] init];
    [self.navigationController pushViewController:changeBank animated:YES];
}

- (void)selectBankCard {
    if (userManager.bankModel) {
        _bankInfoView.bankModel = userManager.bankModel;
        return;
    }
    
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_selectBanckCard);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @(userManager.detailUserInfo.userId), @"userId",
                          nil];
    
    [WHNetworkManager requestWithURL:postURL parameters:dict isUpData:YES Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                NSDictionary *retDict = responseObject[@"retobj"];
                userManager.bankModel = [WHBankModel modelWithDictionary:retDict];
                _bankInfoView.bankModel = userManager.bankModel;
                
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
